We’ve finally arrived at creating our first maps based on the data we
imported and cleaned in the previous sessions. The focus of this first
session lies on the package tmap to create maps.
# load libraries
library(tmap)
library(sf)
library(dplyr)
# code based on the original
german_districts <-
sf::read_sf("./data/VG250_KRS.shp") %>%
dplyr::rename(district_id = AGS)
attributes_districts <- readr::read_csv("./data/attributes_districts.csv")
## Rows: 411 Columns: 5
## ── Column specification ─────────────────────────────────
## Delimiter: ","
## chr (1): district_id
## dbl (4): population, death_rate, death7_lk, afd_votes...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
german_districts_enhanced <-
german_districts %>%
dplyr::left_join(attributes_districts, by = "district_id") %>%
sf::st_transform(crs = 3035)
# start by setting the tmap mode to "plot" for static maps
tmap_mode("plot") # interactive maps can be designed by changing the mode to "view"
## tmap mode set to plotting
# first maps based on geometrc features
tm_shape(german_districts_enhanced) + # call the shapefile first
tm_fill(col = "lightgrey") # fills the polygons without drawing borders
# ... or use tm_polygons
tm_shape(german_districts_enhanced) +
tm_polygons(col = "lightblue") # fills the polygons and draws borders
In a second step, we want to visualize some information on the German
districts contained in the attribute table. Choose the column
death_rate and create a map of Covid-19 deaths in Germany.
Alternate the map by:
covid_map.
Combine the following options with a plus sign:
tm_fill(col = "", title = "", palette = "") +tm_layout(title = "", title.color = "") +tm_legend(legend.outside = TRUE/FALSE, legend.outside.position = "")colors(), R returns the names of all built-in
colors.
<-.
# plot the covid-19 cases per 100.000 inhabitants
tm_shape(german_districts_enhanced) +
tm_fill(col = "death_rate") # "col" can be the name of a color or a column name
# change the title of the legend and the color palette
tm_shape(german_districts_enhanced) +
tm_fill(col = "death_rate",
title = "Covid-19 Death Rate", # add a title to the legend
palette = "RdPu") # change the color palette
# add a title in the color blue
tm_shape(german_districts_enhanced) +
tm_fill(col = "death_rate",
title = "Covid-19 Death Rate",
palette = "RdPu") +
tm_layout(title = "Covid Deaths in Germany", # alternate the overall layout like title
title.color = "blue" ) # changes the font color of the title
# place the legend outside of the map to the left
tm_shape(german_districts_enhanced) +
tm_fill(col = "death_rate",
title = "Covid-19 Death Rate",
palette = "RdPu") +
tm_layout(title = "Covid Deaths in Germany",
title.color = "blue" ) +
tm_legend(legend.outside = TRUE, # positions the legend outside
legend.outside.position = "left") # defines legend positions
# save your map in an object called "covid_map"
covid_map <-
tm_shape(german_districts_enhanced) +
tm_fill(col = "death_rate",
title = "Covid-19 Death Rate",
palette = "RdPu") +
tm_layout(main.title = "Covid Deaths in Germany",
main.title.color = "blue" ) +
tm_legend(legend.outside = TRUE,
legend.outside.position = "left")